﻿using AutoRetailsEXE;
using CefSharp.WinForms;
using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AutoRetailsDLL.Workers.TianMao.TianMaoAddGoodModel_In;
using AutoRetailsDLL.Workers.PinDuoDuo;
using Newtonsoft.Json;
using Materialfile = AutoRetailsDLL.Workers.TianMao.TianMaoAddGoodModel_In.Materialfile;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI.WebControls;
using CefSharp.DevTools.Page;
using System.Windows.Forms;

namespace AutoRetailsDLL.Workers.TianMao
{
    public class TianMaoWorker : WorkersBase
    {
        private string web_url = "https://sell.publish.tmall.com/tmall/smart/category.htm";
        private TianMaoAddGoodModel_In param_in = null;
        private static string _count = string.Empty;
        private static string _password = string.Empty;

        public static TianMaoWorker Instance=new TianMaoWorker();
        public void InitPinDuoDuoAsync()
        {
            chromePage.Show();
            GetChromiumWebBrowser().LoadUrl(web_url);
            WaitWebBrowserAsync(2000);
            WaitWebBrowserLoadingAsync();
            //设置上传文件事件
            SetUploadFileDialogHandler();
            GetChromiumWebBrowser().AddressChanged += TianMaoWorker_AddressChanged;
            GetChromiumWebBrowser().FrameLoadStart += ChromiumWebBrowserEx_FrameLoadStart;
            //GetChromiumWebBrowser().FrameLoadEnd += ChromiumWebBrowserEx_FrameLoadEnd;
            //await Login();
        }
        //private void ChromiumWebBrowserEx_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        //{
        //    if (e.Frame.Url.Contains("https://market.m.taobao.com/app/crs-qn/sucai-selector-ng/index"))
        //    {
        //        e.Frame.ExecuteJavaScriptAsync("alert('哈哈')");
        //    }
        //    if (e.Frame.IsMain)
        //    {
        //        // 主页面加载完成
        //    }
        //    else
        //    {
        //        // iframe加载完成
        //    }
        //}

        private void ChromiumWebBrowserEx_FrameLoadStart(object sender, FrameLoadStartEventArgs e)
        {
            if (!e.Frame.IsMain)
            {
                // iframe开始加载
            }
        }
        private void TianMaoWorker_AddressChanged(object sender, AddressChangedEventArgs e)
        {
            var chromiumWebBrowser = (ChromiumWebBrowser)sender;
            var newAddress = chromiumWebBrowser.Address;
            if (Is判断Login状态 && newAddress.Contains("login"))
            {
                Is登录 = false;
            }
            Is登录 = true;
            // 在这里处理新的地址
            //System.Diagnostics.Debug.WriteLine($"New Address: {newAddress}");
        }
        public void SetParam(TianMaoAddGoodModel_In pinDuoDuoAddGoodModel_In)
        {
            param_in = pinDuoDuoAddGoodModel_In;
        }
        public async Task<WorkResult> Login(string count, string password) 
        {
            try
            {
                LogHelper.WriteLog($"天猫开始登陆：{count}，{password}");
                
                InitPinDuoDuoAsync();
                执行必备JS();
                WaitWebBrowserAsync(500);
                GetChromiumWebBrowser().SetInputValue("fm-login-id", count);
                WaitWebBrowserAsync(500);
                GetChromiumWebBrowser().SetInputValue("fm-login-password", password);
                WaitWebBrowserAsync(500);
                GetChromiumWebBrowser().GetElementByTextAndClick("button", "登录");
                for (int i = 0; i < 5; i++)
                {
                    if (IsLogin2())
                    {
                        LogHelper.WriteLog($"天猫登陆成功");
                        result = WorkResult.WorkSuccess();
                    }
                    else
                    {
                        LogHelper.WriteLog($"天猫登陆失败");
                        WaitWebBrowserAsync(1000);
                    }
                }
                result = WorkResult.WorkFailed();
                return result;
            }
            catch (Exception ee)
            {
                LogHelper.WriteLog($"Login发生异常", ee);
                return WorkResult.WorkFailed();
            }
        }
        /// <summary>
        /// 通过刷新页面判断，更准确
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            Is判断Login状态 = true;
            GetChromiumWebBrowser().LoadUrl(web_url);
            WaitWebBrowserLoadingAsync();
            WaitWebBrowserAsync(5000);
            LogHelper.WriteLog($"登录验证：{Is登录}");
            Is判断Login状态 = false;
            return Is登录;
        }
        public async Task<WorkResult> AddGood(string json, string configJson)
        {
            result = WorkResult.WorkFailed();
            try
            {
                if (DateTime.Now > new DateTime(2024, 10, 12))
                {
                    LogHelper.WriteLog($"开始AddGood：控件已失效");
                    result = WorkResult.WorkFailed();
                    return result;
                }
                bool login_result = IsLogin();
                if (!login_result)
                {
                    result = WorkResult.WorkFailed("请先登录");
                    return result;
                }
                LogHelper.WriteLog($"AddGood入参：{json}");
                LogHelper.WriteLog($"AddGood配置入参：{configJson}");
                SetConfig(configJson);
                TianMaoAddGoodModel_In pinDuoDuoAddGoodModel_In = JsonConvert.DeserializeObject<TianMaoAddGoodModel_In>(json);
                param_in = pinDuoDuoAddGoodModel_In;
                AddGood();
                return result;
            }
            catch (Exception ee)
            {
                LogHelper.WriteLog($"AddGood异常", ee);
                return WorkResult.WorkFailed($"AddGood异常:{ee}");
            }

        }
        public WorkResult AddGood(TianMaoAddGoodModel_In pinDuoDuoAddGoodModel_In, WorkerConfig workerConfig)
        {
            result = WorkResult.WorkSuccess();
            try
            {
                LogHelper.WriteLog($"AddGood入参：{JsonConvert.SerializeObject(pinDuoDuoAddGoodModel_In)}");
                LogHelper.WriteLog($"AddGood配置入参：{JsonConvert.SerializeObject(pinDuoDuoAddGoodModel_In)}");
                SetConfig(workerConfig);
                param_in = pinDuoDuoAddGoodModel_In;
                AddGood();
                LogHelper.WriteLog($"任务结束出参：{JsonConvert.SerializeObject(result)}");
                return result;
            }
            catch (Exception ee)
            {
                LogHelper.WriteLog($"AddGood异常", ee);
                return WorkResult.WorkFailed($"AddGood异常:{ee}");
            }

        }
        public void  AddGood()
        {
            滚动屏幕();
            执行必备JS();
            上传商业主图();
            bool check_result = 确认商品类目();
            if (!check_result)
            {
                return;
            }
            //页面跳转后原JS失效，需要再次执行
            执行必备JS();
            check_result=填写产品参数();
            if (!check_result)
            {
                return;
            }
            check_result = 填写重要属性();
            if (!check_result)
            {
                return;
            }
            填写其他属性();
            填写销售信息();
            填写规格();
            商品物流信息();
            图文描述();
            check_result=上传详情图();
            if (!check_result)
            {
                return;
            }
            点击确认();
        }
        private WorkResult 上传商业主图() 
        {
            LogHelper.WriteLog($"上传商业主图");
            List<string> file_path_list = param_in.materialFile.FirstOrDefault(n => n.materialType == "master").specList.Select(n => n.imageName).ToList();// new List<string> { @"E:\谷歌下载\1.jpeg", @"E:\谷歌下载\2.jpeg", @"E:\谷歌下载\3.jpeg" };
            foreach (var item in file_path_list)
            {
                点击屏幕();
                WaitWebBrowserAsync(200);
                SetUploadFilePath(new List<string>() { item } );
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击上传主图JS, 2000);
            }
            LogHelper.WriteLog($"上传商业主图结束");
            return WorkResult.WorkSuccess();
        }
        private  bool 确认商品类目() 
        {
            LogHelper.WriteLog($"确认商品类目");
            if (Is快捷选择品类按钮())
            {
                LogHelper.WriteLog($"点击快捷品类");
                GetChromiumWebBrowser().ExecuteScriptAsync(JavaScriptString.天猫_点击快捷品类JS);
            }
            else
            {
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_3C数码配件JS, 1000);
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_手机配件JS, 1000);
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_手机保护套JS, 1000);
                
            }
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击品牌输入框JS, 1000);
            string js = string.Format(JavaScriptString.天猫_输入品牌JS, param_in.brand);
            ExecuteScriptAndWaitAsync(js, 1000);
            js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, param_in.brand);
            string check_result= EvaluateScriptAsync(js);
            if (check_result!="True")
            {
                LogHelper.WriteLog($"品牌未找到:{param_in.brand}");
                result = WorkResult.WorkFailed($"品牌未找到:{param_in.brand}");
                return false;
            }
            js = string.Format(JavaScriptString.天猫_选择品牌JS, param_in.brand);
            ExecuteScriptAndWaitAsync(js, 1000);

            js = string.Format(JavaScriptString.天猫_输入型号JS, param_in.modelCode[0].tianMaoApplyModel);
            ExecuteScriptAndWaitAsync(js, 2000);
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击下一步完善商品信息JS, 5000);
            return true;
        }
        private bool 填写产品参数() 
        {
            //等待进入商品详情页面
            for (int i = 0; i < 20; i++)
            {
                string result = EvaluateScriptAsync(JavaScriptString.天猫_判断是否进入到商品详情页面JS);
                if (result=="False")
                {
                    WaitWebBrowserAsync(500);
                }
                else
                {
                    break;
                }
                
            }
            //风格
            LogHelper.WriteLog($"风格设置");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击风格设置, 1000);
            string js = string.Format(JavaScriptString.天猫_选择风格,param_in.goodsStyle);
            ExecuteScriptAndWaitAsync(js, 1000);
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_确认风格, 1000);

            //款式
            LogHelper.WriteLog($"款式设置");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击款式, 1000);
            js = string.Format(JavaScriptString.天猫_搜索款式, param_in.goodsDesign);
            ExecuteScriptAndWaitAsync(js, 1000);
            js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, param_in.goodsDesign);
            string check_result = EvaluateScriptAsync(js);
            if (check_result != "True")
            {
                result = WorkResult.WorkFailed($"款式未找到:{param_in.goodsDesign}");
                return false;
            }
            js = string.Format(JavaScriptString.天猫_选择款式,param_in.goodsDesign);
            ExecuteScriptAndWaitAsync(js, 1000);

            //适用手机机型
            LogHelper.WriteLog($"适用手机机型");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击适用手机机型, 500);
            js = string.Format(JavaScriptString.天猫_搜索适用手机机型, param_in.modelCode[0].tianMaoApplyBrand);
            ExecuteScriptAndWaitAsync(js, 500);
            js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, param_in.modelCode[0].tianMaoApplyBrand);
            check_result = EvaluateScriptAsync(js);
            if (check_result != "True")
            {
                LogHelper.WriteLog(($"手机机型未找到:{param_in.goodsDesign}"));
                result = WorkResult.WorkFailed($"手机机型未找到:{param_in.goodsDesign}");
                return false;
            }
            js = string.Format(JavaScriptString.天猫_选择适用手机机型, param_in.modelCode[0].tianMaoApplyBrand);
            ExecuteScriptAndWaitAsync(js, 500);

            //填写型号
            LogHelper.WriteLog($"填写型号");
            js = string.Format(JavaScriptString.天猫_填写型号, param_in.modelCode[0].tianMaoApplyModel);
            ExecuteScriptAndWaitAsync(js, 500);
            //保护套质地
            LogHelper.WriteLog($"填写保护套质地");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击保护套质地, 500);
            js = string.Format(JavaScriptString.天猫_搜索保护套质地, param_in.goodsTexture);
            ExecuteScriptAndWaitAsync(js, 500);
            js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, param_in.goodsTexture);
            check_result = EvaluateScriptAsync(js);
            if (check_result != "True")
            {
                LogHelper.WriteLog($"保护套质地未找到:{param_in.goodsTexture}");
                result = WorkResult.WorkFailed($"保护套质地未找到:{param_in.goodsTexture}");
                return false;
            }
            js = string.Format(JavaScriptString.天猫_选择保护套质地, param_in.goodsTexture);
            ExecuteScriptAndWaitAsync(js, 500);

            return true;
        }
        private bool 填写重要属性() 
        {
            LogHelper.WriteLog($"填写重要属性");
            //填写流行元素
            LogHelper.WriteLog($"填写流行元素");
            string js = string.Format(JavaScriptString.天猫_填写流行元素, param_in.fashionElement);
            ExecuteScriptAndWaitAsync(js, 500);

            //是否可降解
            LogHelper.WriteLog($"是否可降解");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击是否可降解, 500);
            js = string.Format(JavaScriptString.天猫_选择是否可降解, param_in.otherInfo.是否可降解);
            ExecuteScriptAndWaitAsync(js, 500);

            //填写生产企业
            LogHelper.WriteLog($"填写生产企业");
            js = string.Format(JavaScriptString.天猫_填写生产企业, param_in.productionCompany);
            ExecuteScriptAndWaitAsync(js, 500);

            //是否使用环保材质
            LogHelper.WriteLog($"是否使用环保材质");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击是否使用环保材质, 500);
            js = string.Format(JavaScriptString.天猫_选择是否使用环保材质, param_in.otherInfo.是否使用环保材质);
            ExecuteScriptAndWaitAsync(js, 500);

            //功能
            LogHelper.WriteLog($"功能");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击功能, 500);
            js = string.Format(JavaScriptString.天猫_搜索功能, param_in.function);
            ExecuteScriptAndWaitAsync(js, 500);
            js = string.Format(JavaScriptString.天猫_选择功能, param_in.function);
            ExecuteScriptAndWaitAsync(js, 500);
            return true;
        }
        private bool 填写其他属性() 
        {
            LogHelper.WriteLog($"填写其他属性");
            //厚度
            string js = string.Format(JavaScriptString.天猫_填写厚度, param_in.thickness);
            ExecuteScriptAndWaitAsync(js, 500);
            //是否支持5G信号
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击是否支持5G信号, 500);
            js = string.Format(JavaScriptString.天猫_选择是否支持5G信号, param_in.otherInfo.是否支持5G信号);
            ExecuteScriptAndWaitAsync(js, 500);
            LogHelper.WriteLog($"填写其他属性完成");
            return true;
        }
        private bool 填写销售信息() 
        {
            LogHelper.WriteLog($"填写销售信息");
            //适用手机型号
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击适用手机型号, 500);
            //for (int i = 0; i < param_in.spec1.Count; i++)
            //{
            //    string js = string.Format(JavaScriptString.天猫_搜索适用手机型号, param_in.spec1[i]);
            //    ExecuteScriptAndWaitAsync(js, 500);
            //    js = string.Format(JavaScriptString.天猫_选择适用手机型号, param_in.spec1[i]);
            //    ExecuteScriptAndWaitAsync(js, 500);
            //}
            foreach (var item in param_in.spec1) 
            {
                string js = string.Format(JavaScriptString.天猫_搜索适用手机型号, item);
                ExecuteScriptAndWaitAsync(js, 1000);
                js = string.Format(JavaScriptString.天猫_进入手机型号, item);
                ExecuteScriptAndWaitAsync(js, 1000);
                if (Is选项存在(item))
                {
                    js = string.Format(JavaScriptString.天猫_选择适用手机型号, item);
                    ExecuteScriptAndWaitAsync(js, 1000);
                }
                else
                {
                    result = WorkResult.WorkFailed($"适用手机型号未找到：{item}");
                    return false;
                }
                
            }
            点击屏幕();
            //
            for (int i = 0; i < param_in.spec2.Count; i++)
            {
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击新增颜色, 500);
                string js = string.Format(JavaScriptString.天猫_选择输入颜色,i*2 ,param_in.spec2[i]);
                ExecuteScriptAndWaitAsync(js, 500);
            }
            //发货时间
            if (param_in.shipmentTime=="24小时")
            {
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击今日发货, 500);
            }
            if (param_in.shipmentTime == "48小时")
            {
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击48小时发货, 500);
            }
            //一口价
            string js2 = string.Format(JavaScriptString.天猫_输入一口价, param_in.goodsReferencePrice);
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_输入一口价, 500);
            LogHelper.WriteLog($"填写销售信息完成");
            return true;
        }
        private bool Is选项存在(string content)
        {
            string js_check = @" (function(){
                return getElementByText('li','" + content + @"')!=null;
            })() ";
            string result = EvaluateScriptAsync(js_check);
            if (result == "False")
            {
                return false;
            }
            return true;
        }
        private void 填写规格() 
        {
            LogHelper.WriteLog($"填写规格");
            string 设置speclist变量js = @"var ele =document.getElementsByClassName('sell-sku-body-table')[0].getElementsByTagName('tr')[{0}].getElementsByTagName('input')[{1}];setInputValue(ele,'{2}')";
            string 上传图片js= @"var ele =document.getElementsByClassName('sell-sku-body-table')[0].getElementsByTagName('tr')[{0}];ele.getElementsByClassName('next-icon')[0].click()";
            foreach (Materialfile item in param_in.materialFile)
            {
                if (item.materialType == "sku")
                {
                    for (int i = 0; i < item.specList.Count; i++)
                    {
                        string js = string.Format(设置speclist变量js, i, 0, item.specList[i].price);
                        ExecuteScriptAndWaitAsync(js, 500);
                        string js2 = string.Format(设置speclist变量js, i, 1, item.specList[i].stockNum);
                        ExecuteScriptAndWaitAsync(js2, 500);
                        string js3 = string.Format(上传图片js, i);
                        ExecuteScriptAndWaitAsync(js3, 3000);
                        上传图片(new List<string>() { item.specList[i].imageName });
                        //string js3 = string.Format(设置speclist变量js, i, 2, item.specList[i].skuCode);
                        //ExecuteScriptAndWaitAsync(js3, 500);
                        //string js4 = string.Format(设置speclist变量js, i, 3, item.specList[i].textureCode);
                        //ExecuteScriptAndWaitAsync(js4, 500);
                    }
                }
            }
            if (!string.IsNullOrEmpty(param_in.businessCode))
            {
                string 天猫_输入商家编码 = string.Format(JavaScriptString.天猫_输入商家编码, param_in.businessCode);
                ExecuteScriptAndWaitAsync(天猫_输入商家编码, 500);
            }
            if (!string.IsNullOrEmpty(param_in.barCode))
            {
                string 天猫_输入条形码 = string.Format(JavaScriptString.天猫_输入条形码, param_in.barCode);
                ExecuteScriptAndWaitAsync(天猫_输入条形码, 500);
            }

            //满两件折扣
            if (!string.IsNullOrEmpty(param_in.fullDiscount) )
            {
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击启用多件优惠, 500);
                string 天猫_输入多件折扣 = string.Format(JavaScriptString.天猫_输入多件折扣, param_in.fullDiscount);
                ExecuteScriptAndWaitAsync(天猫_输入多件折扣, 500);
            }
            LogHelper.WriteLog($"填写规格完成");
        }
        private void 商品物流信息() 
        {
            LogHelper.WriteLog($"填写商品物流信息");
            string 天猫_输入物流体积 = string.Format(JavaScriptString.天猫_输入物流体积, param_in.volume);
            ExecuteScriptAndWaitAsync(天猫_输入物流体积, 500);
            string 天猫_输入物流重量 = string.Format(JavaScriptString.天猫_输入物流重量, param_in.weight);
            ExecuteScriptAndWaitAsync(天猫_输入物流重量, 500);
            List<string> localList = param_in.wareLocation.Split('/').ToList();
            if (localList[0]=="大陆及港澳台")
            {
                string 天猫_所在地点击大陆及港澳台 = string.Format(JavaScriptString.天猫_所在地点击大陆及港澳台, param_in.DLJGAT);
                ExecuteScriptAndWaitAsync(天猫_所在地点击大陆及港澳台, 500);
                //选择省份
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击所在地省份, 500);
                string js = string.Format(JavaScriptString.天猫_搜索所在地省份, localList[1]);
                ExecuteScriptAndWaitAsync(js, 500);
                js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, localList[1]);
                string check_result = EvaluateScriptAsync(js);
                if (check_result != "True")
                {
                    LogHelper.WriteLog($"所在地省份未找到:{localList[1]}");
                    result = WorkResult.WorkFailed($"所在地省份未找到:{localList[1]}");
                    return;
                }
                js = string.Format(JavaScriptString.天猫_选择所在地省份, localList[1]);
                ExecuteScriptAndWaitAsync(js, 500);
                //选择城市
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击所在地城市, 500);
                js = string.Format(JavaScriptString.天猫_搜索所在地城市, localList[2]);
                ExecuteScriptAndWaitAsync(js, 500);
                js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, localList[2]);
                check_result = EvaluateScriptAsync(js);
                if (check_result != "True")
                {
                    LogHelper.WriteLog($"所在地城市未找到:{localList[2]}");
                    result = WorkResult.WorkFailed($"所在地城市未找到:{localList[2]}");
                    return;
                }
                js = string.Format(JavaScriptString.天猫_选择所在地城市, localList[2]);
                ExecuteScriptAndWaitAsync(js, 500);
            }
            else 
            {
                string 天猫_所在地点击其他国家或地区 = string.Format(JavaScriptString.天猫_所在地点击其他国家或地区);
                ExecuteScriptAndWaitAsync(天猫_所在地点击其他国家或地区, 500);
                //选择省份
                ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击所在地省份, 500);
                string js = string.Format(JavaScriptString.天猫_搜索所在地省份, localList[1]);
                ExecuteScriptAndWaitAsync(js, 500);
                js = string.Format(JavaScriptString.天猫_判断品牌是否存在JS, localList[1]);
                string check_result = EvaluateScriptAsync(js);
                if (check_result != "True")
                {
                    LogHelper.WriteLog($"所在地其他国家或地区未找到:{localList[1]}");
                    result = WorkResult.WorkFailed($"所在地其他国家或地区未找到:{localList[1]}");
                    return;
                }
                js = string.Format(JavaScriptString.天猫_选择所在地省份, localList[1]);
                ExecuteScriptAndWaitAsync(js, 500);
            }
           

            string 天猫_提取方式点击邮寄 = string.Format(JavaScriptString.天猫_提取方式点击邮寄, param_in.mail);
            ExecuteScriptAndWaitAsync(天猫_提取方式点击邮寄, 500);
            LogHelper.WriteLog($"填写商品物流信息完成");
        }
        private void 图文描述()
        {
            LogHelper.WriteLog($"填写图文描述");
            string 天猫_输入商品标题 = string.Format(JavaScriptString.天猫_输入商品标题, param_in.onlineGoodsTitle);
            ExecuteScriptAndWaitAsync(天猫_输入商品标题, 500);
            string 天猫_输入电脑端描述 = string.Format(JavaScriptString.天猫_输入电脑端描述, param_in.discription);
            ExecuteScriptAndWaitAsync(天猫_输入电脑端描述, 500);
            LogHelper.WriteLog($"填写图文描述完成");

        }
        private bool 上传详情图() 
        {
            LogHelper.WriteLog($"上传详情图");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击详情描述_图片,2000);
            List<string> names= GetChromiumWebBrowser().GetBrowser().GetFrameNames();
            IFrame frame = null;
            foreach (string name in names) 
            {
                frame= GetChromiumWebBrowser().GetBrowser().GetFrame(name);
                if (frame .Url.Contains("https://market.m.taobao.com/app/crs-qn/sucai-selector-ng/index")) 
                {
                    break;
                }
            }
            //ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击详情描述_图片, 500);
            frame.ExecuteJavaScriptAsync(JavaScriptString.getElementByText);
            WaitWebBrowserAsync(500);
            frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_上传图片);
            WaitWebBrowserAsync(1000);
            //ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击详情描述_上传图片, 500);
            List<string> file_path_list = param_in.materialFile.FirstOrDefault(n => n.materialType == "detail").specList.Select(n => n.imageName).ToList();
            SetUploadFilePath(file_path_list);
            点击屏幕中央();
            WaitWebBrowserAsync(200);
            frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_上传);
            WaitWebBrowserAsync(1000);
            for (int i = 0; i < 25; i++)
            {
                var checktask= frame.EvaluateScriptAsync(JavaScriptString.天猫_获取有上传图片有几个DIV);
                checktask.Wait();
                string check_result = checktask.Result?.Result?.ToString();//  EvaluateScriptAsync(JavaScriptString.天猫_获取有上传图片有几个DIV);
                if (check_result == "2")
                {
                    LogHelper.WriteLog($"上传详情图完成");
                    WaitWebBrowserAsync(3000);
                    string js = string.Format(JavaScriptString.天猫_选择上传图片, file_path_list.Count);
                    frame.ExecuteJavaScriptAsync(js);
                    WaitWebBrowserAsync(3000);
                    frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_确认上传);
                    WaitWebBrowserAsync(2000);
                    return true;
                }
                else
                {
                    WaitWebBrowserAsync(1000);
                }
            }
            result = WorkResult.WorkFailed("上传详情图超时");
            return false;
            
        }
        private bool 上传图片(List<string> paths) 
        {
            LogHelper.WriteLog($"上传详情图");
            List<string> names = GetChromiumWebBrowser().GetBrowser().GetFrameNames();
            IFrame frame = null;
            foreach (string name in names)
            {
                frame = GetChromiumWebBrowser().GetBrowser().GetFrame(name);
                if (frame.Url.Contains("https://market.m.taobao.com/app/crs-qn/sucai-selector-ng/index"))
                {
                    break;
                }
            }
            //ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击详情描述_图片, 500);
            frame.ExecuteJavaScriptAsync(JavaScriptString.getElementByText);
            WaitWebBrowserAsync(500);
            frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_上传图片);
            WaitWebBrowserAsync(1000);
            //ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击详情描述_上传图片, 500);
            List<string> file_path_list = paths;
            SetUploadFilePath(file_path_list);
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_设置上传框居中);
            WaitWebBrowserAsync(2000);
            点击屏幕最上边中间();
            WaitWebBrowserAsync(200);
            frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_上传);
            WaitWebBrowserAsync(1000);
            for (int i = 0; i < 25; i++)
            {
                var checktask = frame.EvaluateScriptAsync(JavaScriptString.天猫_获取有上传图片有几个DIV);
                checktask.Wait();
                string check_result = checktask.Result?.Result?.ToString();//  EvaluateScriptAsync(JavaScriptString.天猫_获取有上传图片有几个DIV);
                if (check_result == "2")
                {
                    LogHelper.WriteLog($"上传详情图完成");
                    WaitWebBrowserAsync(3000);
                    string js = string.Format(JavaScriptString.天猫_选择上传图片, file_path_list.Count);
                    frame.ExecuteJavaScriptAsync(js);
                    WaitWebBrowserAsync(3000);
                    frame.ExecuteJavaScriptAsync(JavaScriptString.天猫_点击详情描述_确认上传);
                    WaitWebBrowserAsync(2000);
                    return true;
                }
                else
                {
                    WaitWebBrowserAsync(1000);
                }
            }
            result = WorkResult.WorkFailed("上传详情图超时");
            return false;
        }
        public void 执行命令2(string js)
        {
            List<string> names = GetChromiumWebBrowser().GetBrowser().GetFrameNames();
            IFrame frame = null;
            foreach (string name in names)
            {
                frame = GetChromiumWebBrowser().GetBrowser().GetFrame(name);
                if (frame.Url.Contains("https://market.m.taobao.com/app/crs-qn/sucai-selector-ng/index"))
                {
                    break;
                }
            }
            var task = frame.EvaluateScriptAsync(js);
            task.Wait();
            string result = task.Result?.Result?.ToString();
            MessageBox.Show(result);
        }
       
        
        
        private void 点击确认()
        {
            LogHelper.WriteLog($"点击确认");
            ExecuteScriptAndWaitAsync(JavaScriptString.天猫_点击提交, 500);
            for (int i = 0; i < 10; i++)
            {
                string checkresult = EvaluateScriptAsync(JavaScriptString.天猫_判断是否成功提交JS);
                LogHelper.WriteLog($"正式提交结果判断：{checkresult}");
                if (checkresult!=null&&checkresult.Contains("宝贝id"))
                {
                    var uri = new Uri(GetChromiumWebBrowser().Address);
                    NameValueCollection collection = HttpUtility.ParseQueryString(uri.Query);
                    result = WorkResult.WorkSuccess(collection["primaryId"]);
                    return;
                }
                WaitWebBrowserAsync(1000);
            }
            result = WorkResult.WorkFailed("检查提交是否成功超时");
            
            LogHelper.WriteLog($"点击确认完成");

        }


        private bool Is快捷选择品类按钮()
        {
            string result = EvaluateScriptAsync(JavaScriptString.天猫_获取是否有快捷品类JS);
            if (result == "True")
            {
                return true;
            }
            return false;
        }

    }
    
}
